package com.powergisol.gis.controller;


import com.powergisol.gis.entity.TabPipeStaticColor;
import com.powergisol.gis.exception.GlobalExceptionHandler;
import com.powergisol.gis.service.TabDataSettingService;
import com.powergisol.gis.service.TabPipeStaticColorService;
import com.powergisol.gis.vo.*;
import com.powergisol.gis.service.TabPipeService;
import com.powergisol.gis.util.Result;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.util.List;


@RestController
@RequestMapping("/pipe")
public class TabPipeController extends BaseController{

    @Autowired
    private TabPipeService tabPipeService;
    @Autowired
    private TabPipeStaticColorService colorService;


    /**
     * 管网导入数据
     *
     * @param file 文件
     * @param vo   参数
     * @param p
     * @return
     */
    @RequestMapping(value = "/importfile", method = RequestMethod.POST)
    public Result importFile(MultipartFile file, String vo, Principal p) {
        ObjectMapper mapper = new ObjectMapper();
        WangXianVO wangXianVO = null;
        try {
            wangXianVO = mapper.readValue(vo, WangXianVO.class);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        TabUserVo tabUserVo = getUser(p);
        return tabPipeService.importFile(file, wangXianVO, tabUserVo);

    }

    /**
     * 管线导出
     *
     * @param exportPipeVo 传参对象
     * @param response     响应
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public Result exportFile(ExportPipeVo exportPipeVo, HttpServletResponse response) {
        Result result = new Result();
        try {
            tabPipeService.exportFile(exportPipeVo, response);
            result.success();
        } catch (Exception e) {
            throw new GlobalExceptionHandler(500, e.getMessage());
        }
        return result;
    }

    /**
     * 管线详情查看
     *
     * @param pipeDetailsVO 参数
     * @return
     */
    @PostMapping("/selectDetails")
    public Result selectDetails(@RequestBody PipeDetailsVO pipeDetailsVO) {
        return new Result().success(tabPipeService.selectDetails(pipeDetailsVO));
    }


    /**
     * 设置管线的颜色
     *
     * @param vo
     * @return
     */
    @RequestMapping(value = "/setColor", method = RequestMethod.POST)
    public Result setColor(@RequestBody PipeStaticColorVO vo) {
        return colorService.insertBatch(vo);
    }


    /**
     * 获取管线颜色
     * @param uuid
     * @return
     */
    @RequestMapping(value = "/getColor", method = RequestMethod.GET)
    public Result getColor(String uuid) {
        return colorService.selectAll(uuid);
    }
}
